package com.cgi.ndk.wf.pack;

import com.cgi.ndk.wf.generated.instanceManagementService.InstanceInfoType;
import com.cgi.ndk.wf.generated.instanceManagementService.PaginatedInstanceList;
import com.cgi.ndk.wf.service.TaskService;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author brizat
 */
@RestController
@ExposesResourceFor(Package.class)
@RequestMapping("/package")
public class PackageController {

    @Autowired
    private PackageRepository packageRepository;

    @Autowired
    private PackageResourceAssemblerSupport assemblerSupport;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<PackageResource> save(@RequestBody Package pack) {
        return new ResponseEntity<>(assemblerSupport.toResource(packageRepository.save(pack)), HttpStatus.CREATED);
    }

    @RequestMapping
    public ResponseEntity<List<PackageResource>> packages() {
        return new ResponseEntity<>(packageRepository.findAll()
                .stream()
                .map(pack -> assemblerSupport.toResource(pack))
                .collect(Collectors.toList()), HttpStatus.OK);
    }

    @RequestMapping("/{id}")
    public ResponseEntity<Package> scan(@PathVariable("id") String id) {
        return new ResponseEntity<>(packageRepository.findOne(id), HttpStatus.OK);
    }
}
